package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysRoleMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public List<SysRole> selectRoleList(SysRole role) {
        return roleMapper.selectRoleList(role);
    }

    @Override
    public int insertRole(SysRole role) {
        roleMapper.insertRole(role);
        return insertRoleMenu(role);
    }

    private int insertRoleMenu(SysRole role) {
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : role.getMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setSrId(role.getSrId());
            sysRoleMenu.setSmId(menuId);
            list.add(sysRoleMenu);
        }
        if (list.size() > 0)
            return roleMapper.insertRoleMenu(list);
        return 1;
    }

    @Override
    public int deleteRoles(Long[] roleIds){
        for (Long roleId: roleIds) {
            roleMapper.deleteRole(roleId);
        }
        return roleIds.length;
    }

    @Override
    public int deleteRole(Long roleId) {
        return roleMapper.deleteRole(roleId);
    }

    @Override
    public int updateRole(SysRole role) {
        updateRoleMenu(role);
        return roleMapper.updateRole(role);
    }

    public int updateRoleMenu(SysRole role) {
        Long[] menuIds = role.getMenuIds();
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setSrId(role.getSrId());
            sysRoleMenu.setSmId(menuId);
            list.add(sysRoleMenu);
        }
        roleMapper.deleteRoleMenu(role.getSrId());
        if (list.size() > 0)
            return roleMapper.insertRoleMenu(list);
        return 1;
    }

    @Override
    public SysRole selectRoleById(Long srId) {
        return roleMapper.selectRoleById(srId);
    }

    @Override
    public String checkRoleNameUnique(SysRole role) {
        Long roleId = StringUtils.isNull(role.getSrId()) ? -1L : role.getSrId();
        SysRole info = roleMapper.checkRoleNameUnique(role.getSrTitle());
        if (StringUtils.isNotNull(info) && info.getSrId().longValue() != roleId)
        {
            return UserConstants.ROLE_NAME_NOT_UNIQUE;
        }
        return UserConstants.ROLE_NAME_UNIQUE;
    }

    @Override
    public void checkRoleAllowed(SysRole role) {
        if(StringUtils.isNotNull(role.getSrId()) && role.isAdmin()) {
            throw new BusinessException("不允许操作超级管理员角色！");
        }
    }

    @Override
    public int changeStatus(SysRole role) {
        return roleMapper.updateRole(role);
    }


    @Override
    public int deleteRoleByIds(String ids) {
        Long[] srIds = Convert.toLongArray(ids);
        for (Long srId : srIds)
        {
            checkRoleAllowed(new SysRole(srId));
            SysRole role = selectRoleById(srId);
            if (countUserRoleByRoleId(srId) > 0)
            {
                throw new BusinessException(String.format("%1$s已分配,不能删除", role.getSrTitle()));
            }
        }
        // 删除角色与菜单关联
        roleMapper.deleteRoleMenus(srIds);

        return roleMapper.deleteRoleByIds(srIds);
    }

    @Override
    public List<SysUser> selectAllocatedList(SysUser user) {
        return roleMapper.selectAllocatedList(user);
    }

    @Override
    public List<SysUser> selectUnallocatedList(SysUser user) {
        return roleMapper.selectUnallocatedList(user);
    }

    @Override
    public int insertAuthUsers(Long srId, String uIds) {
        Long[] users = Convert.toLongArray(uIds);
        // 新增用户与角色管理
        List<SysUser> list = new ArrayList<>();
        for (Long uId : users)
        {
            SysUser ur = new SysUser();
            ur.setuId(uId);
            ur.setSrId(srId);
            list.add(ur);
        }
        return roleMapper.batchUserRole(list);
    }


    @Override
    public int deleteAuthUser(SysUser user) {
        return roleMapper.deleteUserRoleInfo(user);
    }

    @Override
    public int deleteAuthUsers(Long srId, String uIds) {
        return roleMapper.deleteUserRoleInfos(srId, Convert.toLongArray(uIds));
    }


    private int countUserRoleByRoleId(Long srId) {
        return roleMapper.countUserRoleByRoleId(srId);
    }
}
